// 18/04/2009 Dennis
class NavigationBar extends Control
{
  ArrayList labels;
  String title;
  int selection;
  boolean wasOver = false;
  
  // 18/04/2009 Dennis
  public NavigationBar()
  {
    labels = new ArrayList();
    x = 0;
    y = 0;
    width = scrW;
    height = 50;
    forecolor = color(255);
    backcolor = color(0);
    title = null;
    selection = -1;
  }
  
  // 18/04/2009 Dennis
  public void draw()
  {
    width = scrW;
    noStroke();
    image(navbg, getX(), getY(), width, 50);
    textFont(font2);
    fill(forecolor);
    int localx = getX();
    int localy = getY();
    int[] widths = new int[labels.size()];
    // draw labels
    for(int i = 0; i < labels.size(); i++)
    {
      String label = (String)labels.get(i);
      int dx = (int)textWidth(label) + 25;
      widths[i] = dx;
      image(nav1, localx, localy, dx, height);
      if(selection == i)
        fill(color(#E00040));
      else
        fill(backcolor);
      text(label, localx + 20, localy + 32);
      
      localx += dx;
    }
    // draw title
    if(title != null)
    {
      int dx = (int)textWidth(title) + 25;
      image(nav3, localx, localy, dx, height);
      image(nav4, localx + dx, localy);
      fill(forecolor);
      text(title, localx + 20, localy + 32);
    }
    localx = getX();
    // draw arrow images
    for(int i = 0; i <= widths.length; i++)
    {
      if(i == widths.length)
      {
        image(nav2, localx, localy);
      }
      else
      {
        image(nav5, localx, localy);
        localx += widths[i];
      }
    }
  }
  
  // 18/04/2009 Dennis
  public boolean getEvent(int mx, int my, int mstate)
  {
    if(!wasOver && mstate != 0)
      return false;
    wasOver = mouseOver(mx, my);
    textFont(font2);
    selection = -1;
    if(mouseOver(mx, my))
    {
      int localx = 10;
      for(int i = 0; i < labels.size(); i++)
      {
        String label = (String)labels.get(i);
        int dx = (int)textWidth(label) + 25;
        if(mx >= localx && mx < localx + dx)
        {
          mouseCursor = 1;
          selection = i;
          break;
        }
        localx += dx;
      }
      if(selection >= 0 && mstate == 2)
      {
        onSelect();
        return true;
      }
    }
    return false;
  }
  
  // 18/04/2009 Dennis
  private void onSelect()
  {
    app.removeForm(labels.size() - selection);
    for(int i = labels.size() - 1; i >= selection; i--)
    {
      removeLabel();
    }
  }
  
  // 18/04/2009 Dennis
  public void addLabel(String label)
  {
    if(label.length() > 15)
      label = label.substring(0, 15) + "...";
    labels.add(label);
  }
  
  // 18/04/2009 Dennis
  public void removeLabel()
  {
    if(labels.size() > 0)
      labels.remove(labels.size() - 1);
  }
}
